import HttpProvider from 'HttpProvider'

/**
 * 无限下拉模块实现
 */
class LiteList extends HttpProvider {
    constructor(url, params = {}) {
        super();

        this.items = [];
        this.url = url; // 请求的的url
        this.loading = false; // 正在加载
        this.hasNextPage = true; // 有下一页下一页
        this.page = 0; // 页码
        this.isEmpty = false; // 是否为空 
        this.params = params; // 请求参数
    }

    // 发起请求
    pushRequest() {
        return new Promise((resolve, reject) => {
            let _self = this;
            if (!_self.hasNextPage || _self.loading) {
                return reject();
            }

            _self.loading = true; // 正在加载加载
            let _pageParams = {
                page: parseInt(_self.page) + 1
            },
                reqParams = Object.assign({}, _pageParams, _self.params);

            _self.doGetRequest(_self.url, reqParams).then((response) => {
                if (typeof response == 'string') {
                    _self.hasNextPage = false;

                    reject(response); // 将错误信息回递给外部逻辑
                } else {
                    _self.loading = false;

                    let responseData = response.msg;
                    if (!responseData || responseData.error) {
                        _self.hasNextPage = false;

                        reject(responseData); // 将错误信息回递给外部逻辑
                    } else {
                        let _total = responseData.totalSize || 0, // 总数
                            _page = responseData.page || 0, // 页码
                            _pageSize = responseData.perPageCount || 1, // 每页条数
                            _totalPage = _pageSize > 0 ? Math.ceil(_total / _pageSize) : 0;

                        _self.page = _page;
                        _self.total = _total;

                        if (_page >= _totalPage) {
                            _self.hasNextPage = false;
                        }

                        if (_total == 0) {
                            _self.hasNextPage = false;
                            _self.isEmpty = true;

                            return resolve(responseData);
                        }

                        // 内存消耗量忽略不计，每个对象的都很小，最多60-70个字节，此处可以忽略。
                        _self.items = _self.items.concat(responseData.rows);
                        resolve(responseData);
                    }
                }
            }, (rejectResponse) => {
                _self.loading = false;

                reject(rejectResponse); // 将错误信息回递给外部逻辑
            })
        })
    }

    // 下拉刷新
    pullDownRefresh() {
        this.resetPage(); // 重置页面信息

        return new Promise((resolve, reject) => {
            this.pushRequest().then((respData) => {
                wx.stopPullDownRefresh();

                resolve(respData);
            }, (reject) => {
                reject(rejectData);
            });
        })
    }

    /**
     * 获取列表数据
     */
    getList() {
        return this.items;
    }

    /**
     * 是否有下一页
     */
    getHasNextPage() {
        return this.hasNextPage;
    }

    /**
     * 是否为空
     */
    getIsEmpty() {
        return this.isEmpty;
    }

    resetPage() {
        this.items = [];
        this.page = 0;
        this.hasNextPage = true;
        this.isEmpty = false
    }

    setParams(params) {
        this.params = params;
    }

    isLoading() {
        return this.loading;
    }

    getListJsons() {
        return {
            rows: this.items,
            hasNextPage: this.hasNextPage,
            isLoading: this.loading,
            isEmpty: this.isEmpty,
            total: this.total
        }
    }

}

export default LiteList;